<?php
class Article_model extends MY_Model
{
	public
		$table = 'categories',
		$table_lang = 'category_lang',		
		$prKey = 'id',
		$prKeyLang = 'pl_ID',
		$frKeyLang = 'pl_page_ID',
		$publishedField = 'category_published',

		$hasMany = array(
			array('table' => 'orders_items', 'fk'=>'oi_product_category'),
			array('table' => 'products_categories', 'fk'=>'pc_category_ID'),
		),
		
		$status = array(
			'1' => 'Active',
			'2' => 'Inactive'
		),
		
		$status2 = array(
			'1' => 'Active',
			'2' => 'Inactive'
		)		
	;

	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'id' 	=> '',
			'name' => T_('Name'),
			'id_parent' => T_('Parent category'),
			'description' => T_('Description'),
			'status' => T_('Status'),
			'alias' => T_('Alias')
		);

		$this->rules = array(
			'name' => "trim|strip_tags|required|max_length[255]|callback___nameUnique",
			'id_parent' => "trim|strip_tags|required|is_natural_no_zero",
			'description' => "trim|max_length[255]",
			'status' => "trim|required|is_natural_no_zero",
			'alias' => "trim|required"
		);
	}

	function insert($data = array())
	{
		$data['created'] = MYSQL_NOW();
		
		parent::insert($data);
	}
	
	function search($config = array())
	{
		$query = $this->db->get($this->page_model->table);

		$config = array_merge(array('rowsOnPage' => 10, 'order' => 'page_ID'), $config);
		
		
		if ($this->input->post('per_page'))
		{
			$config['rowsOnPage'] = $this->input->post('per_page');
			$per_page = 0;
		}		
		else if ($this->input->get('per_page'))
		{
			$per_page = $this->input->get('per_page');
		}
		else
		{
			$per_page = 0;
		}

		$order 		= $this->input->get_post('order');
		$direction 	= $this->input->get_post('direction');
		$search 	= $this->input->get_post('search');
		
		// main sql		
		$sql = '';
		$sql .= ' FROM categories p';
		$sql .= ' WHERE 1 = 1'; 
								
		if ($search)
		{
			$sql .= ' AND p.category_name LIKE ' . $this->db->escape('%' . $search . '%'); 
		}
		
		$sql .= ' GROUP BY page_url';		
		
		switch ($order)
		{
			case 'date':
				$sql .= ' ORDER by p.page_created'; 
				break;
			
			default:
				$sql .= ' ORDER by p.page_ID';
				$direction = 'DESC';
		}
		
		$sql .= ($direction == 'DESC') ? ' DESC' : ' ASC';

		// pagination config
		$config['base_url'] = Xurl_add(Xregenerate_url('per_page,search', true), 'search', $search);
		$config['per_page'] = $config['rowsOnPage'];
		$config['total_rows'] = $this->getRowCount($sql);

		$this->pagination->initialize($config); 
		
		// main sql
		$sql = 'SELECT *' . $sql . sprintf(' LIMIT %d, %d', $per_page, $config['per_page']); 

		$this->data['query'] = $this->db->query($sql);
		$this->data['per_page'] = $config['per_page'];
		$this->data['search'] = $search;
	}
	
	/*function get($id = null)
	{
		$this->db->select('*');
		$this->db->from('pages');
		//$this->db->join('files', 'page_picture_file_ID=file_ID', 'LEFT');
		//$this->db->group_by('page_url');
		$this->db->where('page_ID', $id);
		//$this->db->order_by('category_published'); 

		return $this->db->get()->row_array();
	}*/

	function get($id = null)
	{		
		$this->db->from($this->table);
		//$this->db->join('pages_lang', 'pages.page_ID = pages_lang.pl_page_ID AND language_ID = "'.$this->lang_ID.'"', 'left');
		$this->db->where($this->table.".".$this->prKey, $id);
		$query = $this->db->get();
		
		return $query->row_array();
	}

	function insert_lang($item = array())
	{	
		if (!empty($item))
		{
			$insert_ID = $this->db->insert($this->table_lang, $item); 		
			return $insert_ID;				
		}
	}
		
	function getByUrl($url = '')
	{
		$this->db->select('*');
		$this->db->from('pages');
		$this->db->join('files', 'page_picture_file_ID=file_ID', 'LEFT');
		$this->db->group_by('page_url');
		$this->db->where('page_url', $url);
		$this->db->where('category_published', 1);

		return $this->db->get()->row();
	}		

	function get_versions($url = '')
	{
		$this->db->select('pages.*, user_name');
		$this->db->from('pages');
		$this->db->join('users', 'page_created_user_ID=user_ID', 'LEFT');
		$this->db->join('files', 'page_picture_file_ID=file_ID', 'LEFT');
		$this->db->where('page_url', $url); 
		$this->data['query'] = $this->db->get();
		
		return $this->data['query'];
	}		

	function nameUnique($name = null)
	{
		$this->db->where('name', $name);
		
		if(!empty($this->id_category))
		{
			$this->db->where('id !=', $this->id_category);	
		}
		
		$query = $this->db->get($this->table);

		if (!$query->num_rows())
		{
			return true;
		}		
		
		return false;
	}
}
?>